package com.dragon.modules.yuegou.service.impl.coupon;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.modules.yuegou.entity.coupon.CouponScopeSpuEntity;
import com.dragon.modules.yuegou.mapper.coupon.CouponScopeSpuMapper;
import com.dragon.modules.yuegou.model.product.Spu;
import com.dragon.modules.yuegou.query.coupon.CouponScopeSpuQuery;
import com.dragon.modules.yuegou.service.coupon.CouponScopeSpuService;
import com.dragon.modules.yuegou.service.product.SpuService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
 * 优惠券指定商品 服务实现
 *
 * @author pbuilder generated
 * @date 2019-02-28 18:30:17
 * @version 2.0
 */
@Service("coupCouponScopeSpuService")
public class CouponScopeSpuServiceImpl
		extends ServiceImpl<CouponScopeSpuMapper, CouponScopeSpuEntity>
		implements CouponScopeSpuService {

	@Autowired
	private SpuService spuService;

	@Override
	public List<Spu> getSpuList(String couponTemplateId) {
		List<Spu> list = new ArrayList<>();
		if (StringUtils.isNotEmpty(couponTemplateId)) {
			CouponScopeSpuQuery query = new CouponScopeSpuQuery();
			query.setCouponTemplateId(couponTemplateId);
			List<CouponScopeSpuEntity> scopeSpuList = list(query.toQueryWrapper());
			if (CollectionUtils.isNotEmpty(scopeSpuList)) {
				return scopeSpuList.stream().map(scopeSpu -> new Spu(spuService.getById(scopeSpu.getSpuId()))).collect(Collectors.toList());
			}
		}
		return list;
	}

	@Override
	public List<String> scopeSpuList(String id) {
		CouponScopeSpuQuery query = new CouponScopeSpuQuery();
		query.setCouponTemplateId(id);
		List<CouponScopeSpuEntity> list = list(query.toQueryWrapper());
		return list.stream().map(CouponScopeSpuEntity::getSpuId).collect(Collectors.toList());
	}

}